﻿myApp.controller('AgentSaleController', function ($scope, $http, $routeParams,$location, $rootScope) {
    $scope.isAndroid = $rootScope.isAndroid;

    pageInit();

    $scope.sortOption = "Sorted by Name (A to Z)";


    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }


    $scope.getShareURL = function (p) {
        //  return (BASE_URL + "/property/" + p.propertyid + "/S" + p.Zoning[0]);
        return (BASE_URL + "/property/" + p.propertyid + "/" + p.SaleType);
    }

    $scope.getShareTitle = function (p) {
        return p.ShortHeading;
    }

    $scope.getShareDescription = function (p) {
        return p.LongHeading;
    }

    $scope.getShareImageURL = function (p) {
        return (p.PropertyURL || "https://c1.staticflickr.com/3/2280/32796837675_cd6b733c19_m.jpg");
    }

    $scope.getShareGoogleImageURL = function (p) {
        var url = (p.PropertyURL || "https://c1.staticflickr.com/3/2280/32796837675_cd6b733c19_m.jpg");
        return encodeURI(encodeURI(url));
    }


    $scope.paginate = function (value) {
        var begin, end, index;
        begin = ($scope.currentPage - 1) * $scope.numPerPage;
        end = begin + $scope.numPerPage;
        index = $scope.searchData.indexOf(value);
        return (begin <= index && index < end);
    };

    $scope.isPropertyTransactSale = function (obj) {
        return obj == 'S' ? true : false;
    }


    $scope.updateFilterOption = function (option, sortedChanged) {
        $scope.SortOptionChanged = sortedChanged;
        //$scope.$apply();
        console.log("HEY!" + option);
        switch (option) {
            case 0:
                $scope.sortOption = "Sorted by Name (A to Z)";
                $scope.sortKey = 'Project';
                break;
            case 1:
                $scope.sortOption = "Sorted by Name (Z to A)";
                $scope.sortKey = '-Project';
                break;
        
        }
    }

    $scope.isEmpty = function (obj) {
        for (var i in obj) if (obj.hasOwnProperty(i)) return false;
        return true;
    };


    function pageInit() {
        //var dummyData = [{ "propertyid": "b785a676-5dea-4394-8d16-07468cdd4e0f", "Latitude": 5.385385, "Longitude": 100.382085, "road": "PERAI UTAMA 8 LORONG", "location": "PRAI", "state": "PENANG MAINLAND", "type": "CONDO", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1050.00, "BuildUp": 1050.00, "Project": "PRAI INAI APARTMENT", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 3, "Bathroom": 2, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Residential", "Furnished": "Furnished", "FurnishedRemark": null, "Renovated": "Renovated", "RenovatedRemark": "80K", "Condition": "", "SRAmt": 280000.00, "SCAmt": null, "SIAmt": null, "SLAmt": null, "SalePrice": 280000.00, "UpdatedDate": "2016-07-28T08:25:22.873", "PropertyURL": "https://farm9.staticflickr.com/8836/28483141435_f1415d4b18.jpg", "LongHeading": "CONDOMINIUM, PRAI INAI APARTMENT", "ShortHeading": "CONDO, PRAI INAI APARTMENT", "SaleType": "SR" }, { "propertyid": "ab7ced4b-6688-4864-b013-07dff4836131", "Latitude": 5.400971, "Longitude": 100.368052, "road": "SELAT JALAN     ", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "4/S S/OFF C", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1440.00, "BuildUp": 6360.00, "Project": "TAMAN SELAT", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 1, "Bathroom": 1, "FloorLoad": null, "Ceiling": null, "BestBuy": 1, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": null, "SCAmt": 1300000.00, "SIAmt": null, "SLAmt": null, "SalePrice": 1300000.00, "UpdatedDate": "2016-07-27T09:50:27.807", "PropertyURL": "https://farm2.staticflickr.com/1509/25934267743_89b21fe138.jpg", "LongHeading": "4 STOREY SHOP OFFICE CORNER, TAMAN SELAT", "ShortHeading": "4/S S/OFF C, TAMAN SELAT", "SaleType": "SC" }, { "propertyid": "eab73ab7-806a-41a3-8d30-137a22cf0e8f", "Latitude": 5.422614, "Longitude": 100.395049, "road": "PERUSAHAAN RINGAN 2 LORONG", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "APT", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1210.00, "BuildUp": 1210.00, "Project": "CARISSA PARK", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 4, "Bathroom": 2, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Residential", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Partially Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": 395000.00, "SCAmt": null, "SIAmt": null, "SLAmt": null, "SalePrice": 395000.00, "UpdatedDate": "2016-09-14T13:58:43.057", "PropertyURL": null, "LongHeading": "APARTMENT, CARISSA PARK", "ShortHeading": "APT, CARISSA PARK", "SaleType": "SR" }, { "propertyid": "0ad0df09-ffa8-4cf8-aac9-2545ac24894a", "Latitude": 5.400536, "Longitude": 100.378346, "road": "LIMBUNGAN INDAH JALAN", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "2/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1400.00, "Project": "TAMAN LIMBUNGAN INDAH", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 0, "Bathroom": 0, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": null, "SCAmt": 1100000.00, "SIAmt": null, "SLAmt": null, "SalePrice": 1100000.00, "UpdatedDate": "2016-10-13T12:35:10.4", "PropertyURL": "https://farm9.staticflickr.com/8417/30258456526_8e8eeb858e.jpg", "LongHeading": "DOUBLE-STOREY SHOPHOUSE, TAMAN LIMBUNGAN INDAH", "ShortHeading": "2/S S/HSE, TAMAN LIMBUNGAN INDAH", "SaleType": "SC" }, { "propertyid": "3b76ec5f-3970-4e65-9c1a-2d0bc3de3083", "Latitude": 5.408141, "Longitude": 100.384381, "road": "BAGAN 47 JALAN", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "2/S S/L TER C", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 3980.00, "BuildUp": 3800.00, "Project": "TAMAN BAGAN", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 6, "Bathroom": 4, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Residential", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": 1400000.00, "SCAmt": null, "SIAmt": null, "SLAmt": null, "SalePrice": 1400000.00, "UpdatedDate": "2016-07-20T17:08:36.063", "PropertyURL": "https://farm9.staticflickr.com/8258/28145235040_6d2f7e567d.jpg", "LongHeading": "DOUBLE STOREY SPLIT LEVEL TERRACE CORNER, TAMAN BAGAN", "ShortHeading": "2/S S/L TER C, TAMAN BAGAN", "SaleType": "SR" }, { "propertyid": "6c59af3b-f3de-4489-9c55-651cd78c36ef", "Latitude": 0.000000, "Longitude": 0.000000, "road": "TUN HUSSEIN ONN JALAN   ", "location": "SEBERANG JAYA", "state": "PENANG MAINLAND", "type": "1/S TER", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1000.00, "Project": "SEBERANG JAYA", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 3, "Bathroom": 2, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Leasehold", "Facing": "", "Zoning": "Residential", "Furnished": "Partially Furnished", "FurnishedRemark": null, "Renovated": "Partially Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": 350000.00, "SCAmt": null, "SIAmt": null, "SLAmt": null, "SalePrice": 350000.00, "UpdatedDate": "2016-07-12T14:51:23.323", "PropertyURL": null, "LongHeading": "SINGLE STOREY TERRACE, SEBERANG JAYA", "ShortHeading": "1/S TER, SEBERANG JAYA", "SaleType": "SR" }, { "propertyid": "6a73126e-6c2a-4fe1-b379-7b60a8b26280", "Latitude": 0.000000, "Longitude": 0.000000, "road": "KAMPUNG BENGGALI JALAN    ", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "APT", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 780.00, "BuildUp": 780.00, "Project": "RIA APARTMENT", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 3, "Bathroom": 2, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "N/A", "Zoning": "Residential", "Furnished": "Furnished", "FurnishedRemark": null, "Renovated": "Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": 220000.00, "SCAmt": null, "SIAmt": null, "SLAmt": null, "SalePrice": 220000.00, "UpdatedDate": "2016-09-06T16:46:39.217", "PropertyURL": null, "LongHeading": "APARTMENT, RIA APARTMENT", "ShortHeading": "APT, RIA APARTMENT", "SaleType": "SR" }, { "propertyid": "2a1ec983-71a7-4a70-a223-7e6b7d67d46e", "Latitude": 0.000000, "Longitude": 0.000000, "road": "IMPIANA 6 LORONG", "location": "KULIM", "state": "KEDAH", "type": "2/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1400.00, "Project": "TAMAN LEMBAH IMPIANA", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 0, "Bathroom": 0, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": null, "SCAmt": 560000.00, "SIAmt": null, "SLAmt": null, "SalePrice": 560000.00, "UpdatedDate": "2016-10-12T16:51:06.24", "PropertyURL": null, "LongHeading": "DOUBLE-STOREY SHOPHOUSE, TAMAN LEMBAH IMPIANA", "ShortHeading": "2/S S/HSE, TAMAN LEMBAH IMPIANA", "SaleType": "SC" }, { "propertyid": "d4b95219-bcf7-4c02-80fb-83b004e263d5", "Latitude": 0.000000, "Longitude": 0.000000, "road": "KOTA KENARI 9/2 LORONG", "location": "KULIM", "state": "KEDAH", "type": "2/S TER C", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 3920.00, "BuildUp": 3000.00, "Project": "TAMAN KOTA KENARI", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 6, "Bathroom": 3, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Residential", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Partially Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": 390000.00, "SCAmt": null, "SIAmt": null, "SLAmt": null, "SalePrice": 390000.00, "UpdatedDate": "2016-11-15T13:44:06.193", "PropertyURL": null, "LongHeading": "DOUBLE STOREY TERRACE CORNER, TAMAN KOTA KENARI", "ShortHeading": "2/S TER C, TAMAN KOTA KENARI", "SaleType": "SR" }, { "propertyid": "da9f7f96-857f-4495-ad43-9e5008b7d4d4", "Latitude": 5.377671, "Longitude": 100.395358, "road": "HARUAN 2 MEDAN", "location": "PRAI", "state": "PENANG MAINLAND", "type": "1/S TER", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1200.00, "BuildUp": 1200.00, "Project": "TAMAN SUPREME", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 3, "Bathroom": 2, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Residential", "Furnished": "Partially Furnished", "FurnishedRemark": null, "Renovated": "Partially Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": 370000.00, "SCAmt": null, "SIAmt": null, "SLAmt": null, "SalePrice": 370000.00, "UpdatedDate": "2016-07-25T13:56:00.56", "PropertyURL": "https://farm6.staticflickr.com/5634/24033068471_ffa067ffe0.jpg", "LongHeading": "SINGLE STOREY TERRACE, TAMAN SUPREME", "ShortHeading": "1/S TER, TAMAN SUPREME", "SaleType": "SR" }, { "propertyid": "0fbccb1b-0968-4879-8e0f-df7bac75505a", "Latitude": 0.000000, "Longitude": 0.000000, "road": "KAMPUNG BENGGALI LEBUH    ", "location": "BUTTERWORTH", "state": "PENANG MAINLAND", "type": "2/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 1400.00, "BuildUp": 1400.00, "Project": "KAMPUNG BENGGALI", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 4, "Bathroom": 3, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Partially Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": null, "SCAmt": 680000.00, "SIAmt": null, "SLAmt": null, "SalePrice": 680000.00, "UpdatedDate": "2016-10-25T18:20:27.283", "PropertyURL": null, "LongHeading": "DOUBLE-STOREY SHOPHOUSE, KAMPUNG BENGGALI", "ShortHeading": "2/S S/HSE, KAMPUNG BENGGALI", "SaleType": "SC" }, { "propertyid": "78561ea1-bee0-41ca-99b9-fd01d3e5ecf5", "Latitude": 5.414943, "Longitude": 100.379532, "road": "RAJA UDA JALAN", "location": "RAJA UDA", "state": "PENANG MAINLAND", "type": "3/S S/HSE", "staffid": "d9899bc7-f9b5-4fca-a3b3-e75021ebac77", "agentname": "MONY NG", "mobile": "0124276737", "email": "monyng@lskworld.com", "StaffPicURL": "https://farm4.staticflickr.com/3872/18729593823_21ac95c41d_o.jpg", "LandSqrtFt": 4200.00, "BuildUp": 4200.00, "Project": "WOLLEY", "ProjectRemark": null, "Developer": null, "PropertyDesc": null, "Room": 0, "Bathroom": 0, "FloorLoad": null, "Ceiling": null, "BestBuy": 0, "Tenure": "Freehold", "Facing": "", "Zoning": "Commercial", "Furnished": "Not Furnished", "FurnishedRemark": null, "Renovated": "Not Renovated", "RenovatedRemark": null, "Condition": "", "SRAmt": null, "SCAmt": 1500000.00, "SIAmt": null, "SLAmt": null, "SalePrice": 1500000.00, "UpdatedDate": "2016-10-08T12:59:43.257", "PropertyURL": "https://farm4.staticflickr.com/3703/18824019126_4bdecd8585.jpg", "LongHeading": "THREE-STOREY SHOPHOUSE, WOLLEY", "ShortHeading": "3/S S/HSE, WOLLEY", "SaleType": "SC" }];

        //$scope.searchData = dummyData;

        //$scope.totalItems = $scope.searchData.length;
        //$scope.currentPage = 1;
        //$scope.numPerPage = 10;

        $scope.username = $routeParams.id;
        var url = 'api/Property/Agent/' + $routeParams.id;
        $http.get(url).success(function (data) {
            $scope.searchData = data;
            console.log("AGENT SALE: " + JSON.stringify(data));
            
            $scope.totalItems = $scope.searchData.length;
            $scope.currentPage = 1;
            $scope.numPerPage = 10;

        })
    }

});